﻿using System;
using System . Collections . Generic;
using System . Collections . ObjectModel;
using System . IO;
using System . Linq;
using System . Text;
using System . Threading . Tasks;
using System . Xml . Linq;

namespace WenceyWang . RichMan4L . Stocks
{
	public class StockMarket : GameObject
	{
		public List<Stock> Stocks { get; private set; } = new List<Stock> ( );

		public StockPriceMovement Movement { get; set; }

		public List<Buffs . StockBuff> Buffs { get; private set; } = new List<Buffs . StockBuff> ( );

		Calendars . GameDate movement_changing = Game . Current . Calendar . Today;

		public double SynthesizePrice
		{
			get
			{
				double temp = 0;
				foreach ( var item in Stocks )
				{
					temp += item . Price;
				}
				return temp;
			}
		}

		public override void EndToday ( )
		{
			if ( disposedValue )
			{
				throw new ObjectDisposedException ( nameof ( StockMarket ) );
			}
			foreach ( var item in Stocks )
			{
				item . EndToday ( );
			}
		}

		public override void StartDay ( Calendars . GameDate date )
		{
			if ( disposedValue )
			{
				throw new ObjectDisposedException ( nameof ( StockMarket ) );
			}
			if ( date == movement_changing )
			{
				switch ( Game . Current . Randomer . Next ( 0 , 3 ) )
				{
					case 1:
						{
							Movement = StockPriceMovement . Rise;
							break;
						}
					case 2:
						{
							Movement = StockPriceMovement . Keep;
							break;
						}
					case 3:
						{
							Movement = StockPriceMovement . Fall;
							break;
						}
				}
				foreach ( var item in Stocks )
				{
					item . ChangeAnticipate ( );
				}
				movement_changing += Game . Current . Randomer . Next ( 20 , 30 );
			}
			foreach ( var item in Stocks )
			{
				item . StartDay ( date );
			}
		}

		/// <summary>
		/// 加载资源
		/// </summary>
		/// <param name="flieName"></param>
		public StockMarket ( string flieName ) : this ( ResourceHelper . LoadXmlDocument ( @"" ) )
		{

		}

		public StockMarket ( Stream stream ) : this ( XDocument . Parse ( new StreamReader ( stream ) . ReadToEnd ( ) ) )
		{

		}

		StockMarket ( XDocument document ) : this ( )
		{
			//ToDo LoadStocks

		}

		public StockMarket ( ) : base ( )
		{

		}

	}
}
